﻿using Autofac.Core;
using ManaSky.Apollo;
using ManaSky.Autofac;
using ManaSky.DBCore;
using ManaSky.Core.Attributes;
using ManaSky.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using FreeSql;
using ManaSky.DBCore.Context;
using ManaSky.SSO;

namespace ManaSky.Core
{
    /// <summary>
    /// ManaSky.Core 核心服务
    /// </summary>
    public static class ManaSkyCoreServices
    {
        static Assembly[] Assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "ManaSky.*.dll")
                .Select(Assembly.LoadFrom).ToArray();


        public static void AddManaSkyCoreServices(this IServiceCollection service)
        {
            service.AddControllers(options =>
            {
                //自定义异常处理服务
                options.Filters.Add(new CustomerExceptionFilter());
                //权限处理服务
                options.Filters.Add(new CustomAuthorizationFilterAttribute());
            });
            // AutoMapper服务
            service.AddAutoMapper(Assemblies);
            #region 数据库服务
            service.AddSingleton(FreeSqlContext.GetInstance());
            service.AddFreeRepository();
            service.AddScoped<UnitOfWorkManager>();
            #endregion

            #region jwt
            service.AddJwtService();
            #endregion
        }


        public static void AddManaSkyCoreHost(this IHostBuilder hostBuilder)
        {
            //Apollo
            hostBuilder.AddApolloService();

            //Autofac注入
            var Assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "ManaSky.*.dll")
                .Select(Assembly.LoadFrom).ToArray();
            hostBuilder.AutofacModules(Assemblies);
        }
    }
}
